package com.examp.dy.dy.controller;

import com.examp.dy.dy.dao.UserRepository;
import com.examp.dy.dy.entities.Bloginfo;
import com.examp.dy.dy.entities.Book;
import com.examp.dy.dy.entities.User;
import com.examp.dy.dy.service.BloginfoService;
import com.examp.dy.dy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class blogController {
    @Autowired
    BloginfoService bloginfoService;
    @Autowired
    UserService userService;
    @GetMapping("/blog")
    public String displayBlog(Model model){
        List<Bloginfo> blog = bloginfoService.getAll();
        model.addAttribute("blog",blog);
        return "/main/comment";
    }
    @GetMapping("/blogContent")
    public String displayblogForm(Model model){
       Bloginfo bloginfo= new Bloginfo();
       List<User> users = userService.getAll();
        model.addAttribute("bloginfo",bloginfo);
        model.addAttribute("allUser",users);
        return  "/main/comment";
    }
    @PostMapping("/saveBlog")
    public String creatblog(Model model,Bloginfo bloginfo){
        bloginfoService.save(bloginfo);
        return "redirect:/blogContent";
    }
}
